package com.sip.stream.helper;

import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.util.Log;
import android.view.TextureView;

import com.sip.stream.media.ZFYCameraParameters;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.voice.BeepTool;
import com.sip.stream.utils.zfy.IGSNV12DataSource;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.SingleSubject;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class PreviewSurfaceTextureHelper implements TextureView.SurfaceTextureListener {
    private static final String TAG = "PreviewSurfaceTextureHelper";
    protected SingleSubject<Boolean> isSetUp;
    protected SurfaceTexture surfaceTexture;
    protected TextureView textureView;
    protected int surfaceTextureWidth = 480;
    protected int surfaceTextureHeight = 270;
    protected IGSNV12DataSource videoDataSource = null;
    protected IGSNV12DataSource remoteVideoDataSource = null;
    private AtomicBoolean defaultLocak = new AtomicBoolean();


    public void doAfterCameraOpened() {
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
    }

    public void setVideoDataSource(IGSNV12DataSource iGSNV12DataSource) {
        Log.i(TAG, "setVideoDataSource " + iGSNV12DataSource);
        this.videoDataSource = iGSNV12DataSource;
    }

    public void setRemoteVideoDataSource(IGSNV12DataSource iGSNV12DataSource) {
        Log.i(TAG, "setRemoteVideoDataSource " + iGSNV12DataSource);
        this.remoteVideoDataSource = iGSNV12DataSource;
    }

    public PreviewSurfaceTextureHelper(TextureView textureView) {
        this.textureView = textureView;
    }

    private PreviewSurfaceTextureHelper() {}

    private void setSurfaceTextureDefaultBufferSize(int i, int i2) {
        Log.i(TAG, "setSurfaceTextureDefaultBufferSize: " + i + " x " + i2);
        Log.i(TAG, "setDefaultBufferSize: " + i + " x " + i2);
        this.textureView.getSurfaceTexture().setDefaultBufferSize(i, i2);
    }

    public SurfaceTexture getPreviewSurface() {
        Log.i(TAG, "getPreviewSurface");
        if (this.textureView.getSurfaceTexture() != null) {
            ZFYCameraParameters cameraParameters = ZFYMediaManager.getInstance().getCameraParameters();
            setSurfaceTextureDefaultBufferSize(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight());
        }
        if (this.textureView.isAvailable()) {
            SurfaceTexture surfaceTexture = this.textureView.getSurfaceTexture();
            Log.i(TAG, "return textureView.getSurfaceTexture()");
            return surfaceTexture;
        }
        Log.w(TAG, "!binding.preview.isAvailable()");
        SurfaceTexture surfaceTexture2 = this.surfaceTexture;
        if (surfaceTexture2 != null) {
            return surfaceTexture2;
        }
        Log.e(TAG, "surfaceTexture == null");
        return null;
    }

    public synchronized void setDefaultBufferSize(int i, int i2) {
        TextureView textureView = this.textureView;
        if (textureView != null && textureView.getSurfaceTexture() != null) {
            if (this.defaultLocak.get()) {
                Log.w(TAG, "defaultLocak.get() lock");
                return;
            }
            this.defaultLocak.set(true);
            Single.just(Boolean.TRUE).observeOn(AndroidSchedulers.mainThread()).doOnSuccess(new Consumer() {
                @Override
                public  void accept(Object obj) {
                    if (PreviewSurfaceTextureHelper.this.textureView.getSurfaceTexture() != null) {
                        setSurfaceTextureDefaultBufferSize(i, i2);
                        updateMatrix();
                        return;
                    }
                    Log.w(TAG, "setDefaultBufferSize textureView.getSurfaceTexture() == null");
                }
            }).doFinally(new Action() {
                @Override
                public  void run() {
                    PreviewSurfaceTextureHelper.this.defaultLocak.set(false);
                }
            }).subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onError(Throwable th) {
                }

                @Override
                public void onSubscribe(Disposable disposable) {
                }

                @Override
                public void onSuccess(Boolean bool) {
                }
            });
            return;
        }
        Log.w(TAG, "setDefaultBufferSize null");
    }


    public Single<Boolean> setUpSurface() {
        Log.d(TAG, "setUpSurface");
        if (this.textureView.isAvailable()) {
            Log.d(TAG, "setUpSurface textureView.isAvailable");
            if (this.surfaceTexture == null) {
                this.surfaceTexture = this.textureView.getSurfaceTexture();
            }
            return Single.just(Boolean.TRUE);
        }
        Log.d(TAG, "setUpSurface setSurfaceTextureListener");
        this.textureView.setSurfaceTextureListener(this);
        SingleSubject<Boolean> create = SingleSubject.create();
        this.isSetUp = create;
        return create.timeout(3L, TimeUnit.SECONDS);
    }

    public Single<Boolean> pauseSurface() {
        return Single.just(Boolean.TRUE);
    }

    public Single<Boolean> releaseSurface() {
        return Single.just(Boolean.TRUE).doOnSuccess(new Consumer() {
            @Override
            public  void accept(Object obj) {
            }
        }).onErrorReturnItem(Boolean.TRUE).doFinally(new Action() {
            @Override
            public  void run() {
                Log.i(TAG, "surfaceTexture release");
            }
        });
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
        SurfaceTexture surfaceTexture2 = this.surfaceTexture;
        if (surfaceTexture2 == null) {
            this.surfaceTextureWidth = i;
            this.surfaceTextureHeight = i2;
            Log.i(TAG, "onSurfaceTextureAvailable width" + i + " x " + i2);
            this.surfaceTexture = surfaceTexture;
            if (i == 0 || i2 == 0) {
                BeepTool.beepOneShot();
                Log.e(TAG, "onSurfaceTextureAvailable error");
            } else {
                ZFYCameraParameters cameraParameters = ZFYMediaManager.getInstance().getCameraParameters();
                setDefaultBufferSize(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight());
            }
        } else {
            TextureView textureView = this.textureView;
            if (textureView != null) {
                textureView.setSurfaceTexture(surfaceTexture2);
            } else {
                Log.e(TAG, "textureView == null");
            }
        }
        if (this.isSetUp != null) {
            Log.d(TAG, "isSetUp.onSuccess");
            this.isSetUp.onSuccess(Boolean.TRUE);
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
        this.surfaceTextureWidth = i;
        this.surfaceTextureHeight = i2;
        Log.i(TAG, "onSurfaceTextureSizeChanged width" + i + " x " + i2);
        ZFYCameraParameters cameraParameters = ZFYMediaManager.getInstance().getCameraParameters();
        setDefaultBufferSize(cameraParameters.getVideoWidth(), cameraParameters.getVideoHeight());
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        Log.i(TAG, "onSurfaceTextureDestroyed");
        return false;
    }

    public void updateMatrix() {
        float f = 0;
        float f2;
        float f3 = 0;
        float f4;
        float f5;
        int width = this.textureView.getWidth();
        int height = this.textureView.getHeight();
        if (height == 0 || width == 0) {
            return;
        }
        float f6 = width;
        float f7 = height;
        float f8 = f6 / f7;
        if (height > width) {
            f = (16.0f * f6) / 9.0f;
            f2 = f / f6;
            f3 = f7 / f7;
        } else if (Math.abs(f8 - 1.7777778f) < 0.1f) {
            Log.d(TAG, "not need setTransform");
            this.textureView.setTransform(null);
            return;
        } else if (f8 > 1.7777778f) {
            Log.w(TAG, "> ratio");
            f4 = (9.0f * f6) / 16.0f;
            f2 = f6 / f6;
            f5 = f4 / f7;
            Log.w(TAG, "view(w: " + width + " h: " + height + " r: " + f8 + ") to scale( w: " + f6 + " h: " + f4 + ") scaleX : " + f2 + " scaleY : " + f5);
            Matrix matrix = new Matrix();
            matrix.setScale(f2, f5, 0.0f, 0.0f);
            this.textureView.setTransform(matrix);
        } else {
            Log.w(TAG, "< ratio");
            f = (16.0f * f7) / 9.0f;
            f2 = f / f6;
            f3 = (f7 / f7) + 0.2f;
        }
        f4 = f7;
        f5 = f3;
        f6 = f;
        Log.w(TAG, "view(w: " + width + " h: " + height + " r: " + f8 + ") to scale( w: " + f6 + " h: " + f4 + ") scaleX : " + f2 + " scaleY : " + f5);
        Matrix matrix2 = new Matrix();
        matrix2.setScale(f2, f5, 0.0f, 0.0f);
        this.textureView.setTransform(matrix2);
    }
}
